#ifndef CMS_OS_LINUX_H
#define CMS_OS_LINUX_H

#include <errno.h>
#include <semaphore.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <sys/sysinfo.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>

//typedef u_int64_t  __int64;
typedef u_int64_t UINT64;
typedef u_int16_t UINT16;
typedef u_int64_t UINT48;
typedef u_int64_t CMS_UUID;

#define _vsnprintf vsnprintf
#define _stdcall 

#define MAX_PATH	260
#define SOCKET int
#define BOOL int

typedef struct PACKED{
	char name[20];
	unsigned int user;
	unsigned int nice;
	unsigned int system;
	unsigned int idle;
}CPU_OCCUPY;

typedef struct CMS_OS_MUTEX
{
	pthread_mutex_t* pMutex;
	pthread_mutexattr_t* pMutexAttr;
} CMS_OS_MUTEX;

CMS_OS_MUTEX cms_os_mutex_create();
void cms_os_mutex_delete(CMS_OS_MUTEX m);
void cms_os_mutex_lock(CMS_OS_MUTEX m);
void cms_os_mutex_unlock(CMS_OS_MUTEX m);

class cms_os_mutex_wrapper
{
public:
	cms_os_mutex_wrapper(CMS_OS_MUTEX mutex);
	~cms_os_mutex_wrapper();
private:
	CMS_OS_MUTEX m_mutex;
};

typedef sem_t* CMS_OS_SEM;
CMS_OS_SEM cms_os_sem_create(long maxCount);
void cms_os_sem_delete(CMS_OS_SEM s);
void cms_os_sem_wait(CMS_OS_SEM s);
void cms_os_sem_activate(CMS_OS_SEM s);

typedef pthread_t CMS_OS_THREAD;
typedef pthread_t CMS_OS_TID;
typedef void*(*ThreadProc)(void*);
CMS_OS_THREAD cms_os_thread_create(CMS_OS_TID* pTid, ThreadProc proc, void* pArg);
void cms_os_thread_delete(CMS_OS_THREAD thread);

//自动重置事件
typedef sem_t* CMS_OS_EVENT;
CMS_OS_EVENT cms_os_event_create();
void cms_os_event_delete(CMS_OS_EVENT event);
void cms_os_event_wait(CMS_OS_EVENT event);
void cms_os_event_signal(CMS_OS_EVENT event);

void cms_sleep(int miniseconds);
CMS_UUID cms_generate_uuid();
bool cms_uuid_equal(CMS_UUID u1, CMS_UUID u2);
bool cms_uuid_high_equal(CMS_UUID u1, CMS_UUID u2);
void getcurrentdir(std::string& sPath);
int createDir(const char* sPathName);
int deleteDir(const char* sPathName);
bool fileExists(const char* sfileName);
void getcurrenttime(char* pTime);
std::string getcurrenttime();
void startProcess(const char* cmd);
void terminateProcess(const char* processName);
void cleanFiles(const char* folderName);


void get_cpuoccupy(CPU_OCCUPY* cpust);
int cal_cpu(CPU_OCCUPY* o, CPU_OCCUPY* n);
void get_freemem(long& totalram, long& freeram, int& usedrate);
std::string getCurrentProcessName(const char* exedir);

std::string cms_os_getcurr_path();
std::string cms_os_generateId(std::string serviceName);
CMS_OS_THREAD cms_os_thread_current();

#define HMODULE void*
#define LPCTSTR const char*
#define FARPROC void*
#define BOOL int
#define TRUE 1
#define FALSE 0

BOOL FreeLibrary(HMODULE handle);
FARPROC GetProcAddress(HMODULE handle, LPCTSTR procName);
HMODULE LoadLibrary(char* libPath);

#endif
